package jgl.vajra.driver.widget.view;

import android.content.Context;
import androidx.databinding.BindingAdapter;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import android.util.AttributeSet;

import jgl.vajra.driver.manager.IntentManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import jgl.vajra.driver.R;
import jgl.vajra.driver.widget.utils.ImageUitls;
import jgl.vajra.driver.widget.utils.UIUtils;
import jgl.vajra.driver.widget.giide.GlideImageLoader;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


public class NineGridChildLayout extends NineGridLayout {

    protected static final int MAX_W_H_RATIO = 3;
    private boolean glideFlag;


    public NineGridChildLayout(Context context) {
        super(context);
    }

    public NineGridChildLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected boolean displayOneImage(final RatioImageView imageView, String url, final int parentWidth) {
        if (mContext == null) {
            return false;
        }

        try {
            Glide
                    .with(mContext)
                    .load(url)
                    .apply(GlideImageLoader.getRequestOptions()
                            .placeholder(R.drawable.ic_image_loading)//
                            .error(R.drawable.ic_image_error)//
                            .centerCrop().priority(Priority.LOW))
                    .thumbnail(0.5f)
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            int w = resource.getIntrinsicWidth();
                            int h = resource.getIntrinsicHeight();
                            int newW;
                            int newH;
                            if (h > w * MAX_W_H_RATIO) {//h:w = 5:3
                                newW = parentWidth / 2;
                                newH = newW * 5 / 3;
                            } else if (h < w) {//h:w = 2:3
                                newW = parentWidth * 2 / 3;
                                newH = newW * 2 / 3;
                            } else {//newH:h = newW :w
                                newW = parentWidth / 2;
                                newH = h * newW / w;
                            }
                            setOneImageLayoutParams(imageView, newW, newH);
                            return false;
                        }
                    })
                    .into(imageView);

        } catch (Exception _e) {
            _e.printStackTrace();
        }
        return false;
    }

    @Override
    protected void displayImage(final RatioImageView imageView, final String url) {
        Observable.just(url) // 输入类型 String
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String imgUrl) throws Exception {
                        if (ImageUitls.isGif(imgUrl)) {
                            if (glideFlag) {
                                GlideImageLoader.onDisplayGifUrl(UIUtils.getContext(), imageView, url);
                            } else {
                                GlideImageLoader.onDisplayGifUrl(imageView, url);
                            }
                        } else {
                            Glide.with(UIUtils.getContext()).load(imgUrl)//
                                    .apply(GlideImageLoader.getRequestOptions().override(508, 508).priority(Priority.LOW))
                                    .thumbnail(0.5f)
                                    .into(imageView);
                        }

                    }
                });
    }

    @Override
    protected void onClickImage(int position, String url, List<String> urlList, List<String> attachFiles) {

        IntentManager.toBigImageNetActivity((FragmentActivity) mContext,   attachFiles, position);

    }

    @BindingAdapter(value = {"onNineViewImageList", "onAttachFilesList"}, requireAll = false)
    public static void setNetImage(NineGridChildLayout layout, List<String> minAttachFiles, List<String> attachFiles) {
        if (minAttachFiles == null || minAttachFiles.size() == 0) {
            layout.setVisibility(GONE);
            return;
        }
        layout.setImageList(minAttachFiles,attachFiles);

    }




    public void setImageList(List<String> minAttachFiles, List<String> attachFiles) {

        setUrlList(minAttachFiles,attachFiles);
    }

    public List<String> attachFiles;

    public void setAttachFiles(List<String> attachFiles) {
        this.attachFiles = attachFiles;
    }

    public void setGlideFlag(boolean glideFlag) {
        this.glideFlag = glideFlag;
    }

}
